﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ControlCenter : MonoBehaviour
{
    public int OriginResource;

    public Text FactValue;
    public Text FactValuePlus;
    public Text WeaponValue;
    public Text WeaponValuePlus;
    public Text ContactValue;

    private static ControlCenter _instance;

    public static ControlCenter Instance
    {
        get
        {
            return _instance;
        }
    }

    private ControlCenter()
    {
        _instance = this;
    }

    private int PlayID;

    public int PlayerIdNow
    {
        get
        {
            if (PlayID < 1)
                PlayID = 1;
            return PlayID;
        }
        set
        {
            PlayID = value;
        }
    }

    public int[] Resource;
    public int[] Resource_Plus;
    public int[] Weapon;
    public int[] Weapon_Plus;
    public int[] ContactPoint;
    public int[] WeaponTimes;
    public int[] PlanetsCount;

    [HideInInspector]
    public bool ExistsContactTarget;//全局属性

    [HideInInspector]
    public GameObject ContactTarget;

    private void Start()
    {
        PlayerIdNow = 1;
        InitControlCenter();
        InitControlCenterTotalValueEachTurn();
    }

    public void InitControlCenter()
    {
        for (int i = 0; i < DataCenter.Instance.TotalPlayer; i++)
        {
            Resource[i] = OriginResource - 1;
            Weapon[i] = 0;
            Resource_Plus[i] = 0;
            Weapon_Plus[i] = 0;
            ContactPoint[i] = 1;
            WeaponTimes[i] = 0;
            PlanetsCount[i] = 1;
        }
    }

    private GameObject[] Stars;

    public void InitControlCenterEachTurn()//更新增幅 （在回合变更，产资源建筑变更时更新）
    {
        InitAll();
        Stars = GameObject.FindGameObjectsWithTag("Star");
        foreach (GameObject Star in Stars)
        {
            if (Star.GetComponent<PlanetState>().Resource.HaveRaidar)
                ContactPoint[PlayerIdNow - 1] += 1;
        }
        RefreshPlus();
    }

    public void RefreshPlus()//
    {
        Resource_Plus[PlayerIdNow - 1] = 0;
        Weapon_Plus[PlayerIdNow - 1] = 0;
        ContactTarget = null;
        Stars = GameObject.FindGameObjectsWithTag("Star");
        foreach (GameObject Star in Stars)
        {
            Resource_Plus[PlayerIdNow - 1] += Star.GetComponent<PlanetState>().Resource.industry;
            Weapon_Plus[PlayerIdNow - 1] += Star.GetComponent<PlanetState>().Resource.weapon;
        }
    }

    public void InitAll()//刷新星系数据
    {
        Stars = GameObject.FindGameObjectsWithTag("Star");
        foreach (GameObject Star in Stars)
        {
            Star.GetComponent<PlanetState>().InitState();
        }
    }

    public void InitControlCenterTotalValueEachTurn()//回合变更更新，增加资源
    {
        InitControlCenterEachTurn();
        Resource[PlayerIdNow - 1] += Resource_Plus[PlayerIdNow - 1];
        Weapon[PlayerIdNow - 1] += Weapon_Plus[PlayerIdNow - 1];
    }

    private void Update()
    {
        FactValue.text = Resource[PlayerIdNow - 1].ToString();
        FactValuePlus.text = "+" + Resource_Plus[PlayerIdNow - 1].ToString();
        WeaponValue.text = Weapon[PlayerIdNow - 1].ToString();
        WeaponValuePlus.text = "+" + Weapon_Plus[PlayerIdNow - 1].ToString();
        ContactValue.text = ContactPoint[PlayerIdNow - 1].ToString();
        if (false && Input.GetKeyDown("o"))//变更回合测试
        {
            EndTurn();
        }
    }

    public void InitConmunicateBar()
    {
        Stars = GameObject.FindGameObjectsWithTag("Star");
        foreach (GameObject Star in Stars)
        {
            Star.transform.Find("Comunicate").gameObject.SetActive(false);
        }
    }

    public GameObject Musk;
    private int playerIDTemp;

    public int PlayerIdTemp
    {
        get { return playerIDTemp; }
    }

    public void EndTurn()
    {
        BuyManager.Instance.ShutWindow();
        InteractStarManager.Instance.DestroyTimes = 0;
        Musk.SetActive(true);
        playerIDTemp = PlayerIdNow;
        InitConmunicateBar();
        Stars = GameObject.FindGameObjectsWithTag("Star");
        foreach (GameObject Star in Stars)
        {
            if (Star.GetComponent<PlanetState>().StateNow.Basic == PlanetState.State.BasicState.Observed_empty)
                Star.GetComponent<PlanetState>().StateAll[PlayerIdNow - 1].Basic = PlanetState.State.BasicState.Unknown_Observed;
        }
        PlayerIdNow = 0;
    }

    public void NextPlayer()
    {
        Musk.SetActive(false);
        if (playerIDTemp + 1 > DataCenter.Instance.TotalPlayer)
        {
            PlayerIdNow = 1;
        }
        else
        {
            PlayerIdNow = playerIDTemp;
            PlayerIdNow += 1;
        }
        InitControlCenterTotalValueEachTurn();
    }

    public void ShowMessage(string MessContent)
    {
    }
}